import 'dart:convert';
import 'package:ad_x_sdk/ad_x_sdk.dart';
import 'package:flutter/material.dart';
import 'package:ad_x_sdk_example/com/qlslylq/flutterdesign/scene/BaseScene.dart';
import 'package:flutter/services.dart';
import '../constant/KeyConstant.dart';
import '../util/Log.dart';

/*
 * 插屏广告测试主页 <br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
class InterstitialAdTestMainScene extends BaseScene {
  State<StatefulWidget> createState() {
    return new InterstitialAdTestMainSceneState();
  }
}

/*
 * 页面功能 <br/>
 */
class InterstitialAdTestMainSceneState extends BaseSceneState {
  EventChannel eventChannel = EventChannel('com.qlslylq.ad-x-sdk/adListener');

  InterstitialAdTestMainSceneState() {
    title = '插屏广告测试';
  }

  void initState() {
    super.initState();
    eventChannel.receiveBroadcastStream().listen((dynamic event) {
      String eventName = event['eventName'];
      if (eventName == "onAdShow") {
        Log.dt("onAdShow");
      } else if (eventName == "onAdShowError") {
        var adError = event['error'];
        Log.dt("onAdShowError:" + jsonEncode(adError).toString());
      } else if (eventName == "onAdDismiss") {
        Log.dt("onAdDismiss");
      } else if (eventName == "onAdClick") {
        Log.dt("onAdClick");
      } else if (eventName == "onAdComplete") {
        Log.dt("onAdComplete");
      }
    }, onError: (dynamic error) {}, cancelOnError: true);
    initLoader();
  }

  Widget build(BuildContext context) {
    var listTileHeight = 33.5;
    var listTileMargin = EdgeInsets.fromLTRB(50, 0, 50, 0);

    return buildBody(
        context,
        new Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              new Container(
                width: MediaQuery.of(context).size.width,
                height: listTileHeight,
                margin: listTileMargin,
                child: new RaisedButton(
                  onPressed: () {
                    load();
                  },
                  child: new Text('加载'),
                  color: Theme.of(context).buttonColor,
                  shape: Border.all(
                      color: Colors.red, style: BorderStyle.solid, width: 1),
                ),
              ),
              new Container(
                width: MediaQuery.of(context).size.width,
                height: listTileHeight,
                margin: listTileMargin,
                child: new RaisedButton(
                  onPressed: () {
                    show();
                  },
                  child: new Text('展示'),
                  color: Theme.of(context).buttonColor,
                  shape: Border.all(
                      color: Colors.green, style: BorderStyle.solid, width: 1),
                ),
              ),
              new Container(
                width: MediaQuery.of(context).size.width,
                height: listTileHeight,
                margin: listTileMargin,
                child: new RaisedButton(
                  onPressed: () {
                    loadAndShow();
                  },
                  child: new Text('加载且展示'),
                  color: Theme.of(context).buttonColor,
                  shape: Border.all(
                      color: Colors.blue, style: BorderStyle.solid, width: 1),
                ),
              ),
              new Container(
                width: MediaQuery.of(context).size.width,
                height: listTileHeight,
                margin: listTileMargin,
                child: new RaisedButton(
                  onPressed: () {
                    showToast("正在开发中~");
                  },
                  child: new Text('其它功能'),
                  color: Theme.of(context).buttonColor,
                  shape: Border.all(
                      color: Colors.yellow, style: BorderStyle.solid, width: 1),
                ),
              ),
            ]));
  }

  void onClick(Widget widget) {}

  Future<void> initLoader() async {
    try {
      var param = {
        "JH_INTERSTITIAL_POS_ID": KeyConstant.JH_INTERSTITIAL_POS_ID,
        "autoPlayMuted": true,
        "isHalf": true,
      };
      await AdXSdk.initInterstitialAdLoader(param);
    } on Exception catch (e) {}
  }

  Future<void> load() async {
    try {
      await AdXSdk.loadInterstitialAd();
    } on Exception catch (e) {}
  }

  Future<void> show() async {
    try {
      await AdXSdk.showInterstitialAd();
    } on Exception catch (e) {}
  }

  Future<void> loadAndShow() async {
    try {
      await AdXSdk.loadAndShowInterstitialAd();
    } on Exception catch (e) {}
  }
}
